﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.Modules.Version.Otherfiles
{
    public partial class _ajax : JumbotCms.API.FrontAjax
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!CheckFormUrl())
            {
                Response.Write("提交有误");
                Response.End();
            }

            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "ajaxTrans":
                    ajaxTrans();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = JsonResult(0, "未知操作");
        }
        private void ajaxGetList()
        {
            string uId = "0";
            if (Cookie.GetValue(site.CookiePrev + "user") != null && Str2Str(q("ismine")) == "1")//只能当时人才能检索自己
                uId = Cookie.GetValue(site.CookiePrev + "user", "id");
            int page = Int_ThisPage();
            int PSize = Str2Int(q("pagesize"), 20);
            int countNum = 0;
            string sqlStr = "";
            string joinStr = "A.[UserId]=B.Id";
            string whereStr1 = "A.Id>0";//外围条件(带A.)
            string whereStr2 = "Id>0";//分页条件(不带A.)
            string state = q("state");
            switch (state)
            {
                case "0":
                    whereStr1 += " AND A.State=0";
                    whereStr2 += " AND State=0";
                    break;
                case "1":
                    whereStr1 += " AND A.State=1";
                    whereStr2 += " AND State=1";
                    break;
                case "2":
                    whereStr1 += " AND A.State=2";
                    whereStr2 += " AND State=2";
                    break;
                case "3":
                    whereStr1 += " AND A.State=3";
                    whereStr2 += " AND State=3";
                    break;
            }
            if (uId != "0")
            {
                whereStr1 += " AND A.UserId=" + uId;
                whereStr2 += " AND UserId=" + uId;
            }
            string ccid = Str2Str(q("ccid"));
            string sortid = Str2Str(q("sortid"));
            if (ccid != "0")
            {
                whereStr1 += " AND A.ChannelId=" + ccid;
                whereStr2 += " AND ChannelId=" + ccid;
            }
            if (sortid != "0")
            {
                whereStr1 += " AND A.SortId=" + sortid;
                whereStr2 += " AND SortId=" + sortid;
            }
            doh.Reset();
            doh.ConditionExpress = whereStr2;
            countNum = doh.Count("jcms_module_version_source");
            sqlStr = Jumbot.Common.staticFunction.GetSql("A.id as id,A.Title2 as Title2,B.id as UserId,B.UserName as UserName,B.TrueName as TrueName,A.State as State,A.IsLocked as IsLocked,A.AddDate as AddDate,A.Author2 as Author2,A.SourceFrom2 as SourceFrom2,A.SourceFromUrl2 as SourceFromUrl2,A.AuthorSummary2 as AuthorSummary2,A.Remark as Remark,A.WordCount2 as WordCount2", "jcms_module_version_source", "jcms_normal_user", "Id", PSize, page, "desc", joinStr, whereStr1, whereStr2);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "myuserid :\"" + uId + "\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt, (PSize * (page - 1))) +
                "}";
            dt.Clear();
            dt.Dispose();
        }
        private void ajaxTrans()
        {
            string uId, uName;
            int transCount = 0;
            int maxSelected = Str2Int(Jumbot.Common.XmlCOM.ReadConfig("~/_data/config/user", "MaxSelected"), 1);
            bool isWriter = false;
            if (Cookie.GetValue(site.CookiePrev + "user") != null)
            {
                uId = Cookie.GetValue(site.CookiePrev + "user", "id");
                JumbotCms.DataObject.User _User = new JumbotCms.DataObject.User();
                _User.GetAllColumn(uId, doh);
                uName = _User.UserName;
                isWriter = _User.IsWriter == 1 ? true : false;
                if (!isWriter)
                {
                    this._response = JsonResult(0, "你还没权限翻译");
                    return;
                }
                doh.Reset();
                doh.ConditionExpress = "UserId=@uid and State=1 and IsLocked=0";
                doh.AddConditionParameter("@uid", uId);
                transCount = doh.Count("jcms_module_version_source");
                if (transCount >= maxSelected)
                {
                    this._response = JsonResult(0, "你还有" + transCount + "件稿件未完成,请先专心完成吧");
                    return;
                }
                doh.Reset();
                doh.ConditionExpress = "id=@id and State=0";
                doh.AddConditionParameter("@id", f("id"));
                doh.AddFieldItem("State", 1);
                doh.AddFieldItem("UserId", int.Parse(uId));
                doh.AddFieldItem("Translator", "[" + uName + "]");
                doh.Update("jcms_module_version_source");
                this._response = JsonResult(1, "成功接收任务");
            }
            else
            {
                this._response = JsonResult(0, "请先登录");
            }
        }
    }
}